﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SVC.client;
namespace UI.Forms
{
    public partial class goods : FormBase1
    {
        BindingList<GoodsClass> gc = null;
        string fl = string.Empty;
        public goods()
        {
            InitializeComponent();
        }

        private void goods_Load(object sender, EventArgs e)
        {
            rle_kind.DataSource = my.bw.GCM_Get("", my.ACID);
            rle_kind.DisplayMember = "name";
            rle_kind.ValueMember = "kind";
            TreeNode root = new TreeNode("产品分类");
            gc = my.bw.GoodsClass_Get("ClassID", my.ACID);
            List<GoodsClass> level1 = gc.Where(p => string.IsNullOrWhiteSpace(p.ParentID)).ToList();
            foreach (var top in level1)
            {
                TreeNode topnode = new TreeNode();
                topnode.Text = string.Format("{0}[{1}]",top.ClassID,top.ClassName);
                root.Nodes.Add(topnode);
                getchihild(top.ClassID,topnode);
            }
            treeView1.Nodes.Add(root);
            treeView1.ExpandAll();
        }

        private void getchihild(string classid,TreeNode tn)
        {
            var childs = gc.Where(p => p.ParentID == classid);
            foreach (var child in childs)
            {
                TreeNode childnode = new TreeNode();
                childnode.Text = string.Format("{0}[{1}]", child.ClassID, child.ClassName);
                tn.Nodes.Add(childnode);
                getchihild(child.ClassID,childnode);
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (treeView1.GetNodeAt(e.Location).Text.IndexOf('[') == -1)
            {
                fl = "";
            }
            else
            {
                fl = treeView1.GetNodeAt(e.Location).Text.Split('[')[0];
            }
            
            
            if (!string.IsNullOrWhiteSpace(fl))
            {
                bindgoods(fl);

            }
            
        }

        private void bindgoods(string fl)
        {
            gridControl1.DataSource = my.bw.Goods_Get_byClassid(fl, "GoodsCode", my.ACID);
        }

        private void btn_sx_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bindgoods(fl);
        }

        private void btn_new_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!string.IsNullOrEmpty(fl))
            {

                goods_N frm = new goods_N(fl);
                frm.FatherRefresh = bindgoods;
                frm.ShowDialog();
            }
        }

        private void btn_update_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle>0)
            {
                SVC.client.Goods gd = gridView1.GetRow(gridView1.FocusedRowHandle) as SVC.client.Goods;
            }
        }
    }
}